package com.parking.mgr.useraccount.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.mgr.common.constant.CommonConstant;
import com.parking.mgr.common.exception.ServiceException;
import com.parking.mgr.common.util.IdUtils;
import com.parking.mgr.useraccount.facede.TencentUploadFileComponent;
import com.parking.mgr.useraccount.facede.UserServiceFacede;
import com.parking.mgr.useraccount.model.entity.UserIdFileEntity;
import com.parking.mgr.useraccount.model.entity.UserInfoEntity;
import com.parking.mgr.useraccount.model.mapper.UserIdFileMapper;
import com.parking.mgr.useraccount.model.param.UserIdFileParam;
import com.parking.mgr.useraccount.properties.CosProperties;
import com.parking.mgr.useraccount.service.LoginTokenService;
import com.parking.mgr.useraccount.service.UserIdFileCosService;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @ClassName UserIdFileCosService
 * @Description TODO
 * @author: puyunqing
 * @Date: 2025/3/5
 */
@Service
@Slf4j
public class UserIdFileCosServiceImpl extends ServiceImpl<UserIdFileMapper, UserIdFileEntity> implements UserIdFileCosService {

    @Resource
    private LoginTokenService loginTokenService;

    @Resource
    private UserServiceFacede userServiceFacede;

    @Resource
    private TencentUploadFileComponent tencentUploadFileComponent;


    @Override
    public void uploadFrontFile(MultipartFile file, String fTrueName, String idNumber, HttpServletRequest request) {

        String userPassortId = loginTokenService.queryPassportByClientIdToken(request.getHeader(CommonConstant.LOGIN_TOKEN), request.getHeader(CommonConstant.CLIENT_ID));

        String fileUrl = tencentUploadFileComponent.uploadFile(file);

        if (StrUtil.isBlank(fileUrl)) {
            throw new ServiceException("上传失败");
        }

        // 获取用户信息
        UserInfoEntity userInfoEntity = userServiceFacede.getUserInfoByPassportId(userPassortId);
        if (ObjectUtil.isNull(userInfoEntity)) {
            throw new ServiceException("无用户信息");
        }

        // 更新用户信息
        userInfoEntity.setFTrueName(fTrueName);
        userInfoEntity.setFIdNumber(idNumber);
        userServiceFacede.updateUserInfo(userInfoEntity);

        UserIdFileEntity userIdFileEntity = this.getOneByUserPassportId(userPassortId);
        userIdFileEntity.setFkUserPassportId(userPassortId);
        userIdFileEntity.setFFrontFileId(IdUtils.getId());
        userIdFileEntity.setFFrontFileUrl(fileUrl);

        if (ObjectUtil.isNotNull(userIdFileEntity)) {
            this.updateById(userIdFileEntity);
        } else {
            userIdFileEntity.setId(IdUtils.getId());
            this.save(userIdFileEntity);
        }


    }

    @Override
    public void uploadBackFile(MultipartFile file, String fTrueName, String idNumber, HttpServletRequest request) {

        String userPassortId = loginTokenService.queryPassportByClientIdToken(request.getHeader(CommonConstant.LOGIN_TOKEN), request.getHeader(CommonConstant.CLIENT_ID));

        String fileUrl = tencentUploadFileComponent.uploadFile(file);

        // 获取用户信息
        UserInfoEntity userInfoEntity = userServiceFacede.getUserInfoByPassportId(userPassortId);
        if (ObjectUtil.isNull(userInfoEntity)) {
            throw new ServiceException("无用户信息");
        }

        // 更新用户信息
        userInfoEntity.setFTrueName(fTrueName);
        userInfoEntity.setFIdNumber(idNumber);
        userServiceFacede.updateUserInfo(userInfoEntity);

        UserIdFileEntity userIdFileEntity = this.getOneByUserPassportId(userPassortId);
        userIdFileEntity.setFkUserPassportId(userPassortId);
        userIdFileEntity.setFBackFileId(IdUtils.getId());
        userIdFileEntity.setFBackFileUrl(fileUrl);

        if (ObjectUtil.isNotNull(userIdFileEntity)) {
            this.updateById(userIdFileEntity);
        } else {
            userIdFileEntity.setId(IdUtils.getId());
            this.save(userIdFileEntity);
        }
    }

    @Override
    public UserIdFileEntity getOneByUserPassportId(String userPassportId) {

        LambdaQueryWrapper<UserIdFileEntity> queryWrapper = new LambdaQueryWrapper<UserIdFileEntity>()
                .eq(UserIdFileEntity::getFkUserPassportId, userPassportId);

        return this.getOne(queryWrapper);
    }
}
